package com.example.service.impl;

import com.example.mapper.DeptMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.Dept;
import com.example.pojo.Emp;
import com.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> selectAll() {
        List<Dept> depts = deptMapper.selectAll();
        return depts;
    }

    @Override
    public void add(Dept dept) {
        deptMapper.add(dept);
    }

    @Transactional(rollbackFor = Exception.class)//启用事务异常回滚
    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
        empMapper.deleteByDept(id);
    }

    @Override
    public void uodate(Dept dept) {
        deptMapper.update(dept);
    }

    @Override
    public List<Emp> selectEmp(Integer id) {
        List<Emp> empList = deptMapper.selectEmp(id);
        return empList;
    }
}
